<?php

/*
 * To change this license header, choose License Headers in Project Properties.
 * To change this template file, choose Tools | Templates
 * and open the template in the editor.
 */

class VisitorModel extends CI_Model {

    public function __construct() {
        parent::__construct();
    }

    public function getLog($ip, $time, $time1, $time2, $webId, $serviceId, $rows = 10, $page = 1) {
        if (!empty($time1) && !empty($time2)) {
            $this->db->where("time BETWEEN '$time1' AND '$time2'");
        }
        if (!empty($ip)) {
            $where['ip'] = $ip;
        }
        if (!empty($time)) {
            $where['time'] = $time;
        }
        if (!empty($webId)) {
            $where['cid'] = $webId;
        }
        if (!empty($serviceId)) {
            $where['service_id'] = $serviceId;
        }
        if (!empty($where)) {
            $this->db->where($where);
        }
        $re['total'] = $this->db->get('log')->num_rows();

        if (!empty($where)) {
            $this->db->where($where);
        }
        if (!empty($time1) && !empty($time2)) {
            $this->db->where("time BETWEEN '$time1' AND '$time2'");
        }

        $re['rows'] = $this->db
                ->select(array('id', 'ip', 'cid', 'content', 'filepath', 'service_id', 'service_account', 'sid', 'time'))
                ->limit($rows, $rows * ($page - 1))
                ->get('log')
                ->result_array();
        return $re;
    }

    public function getDownload($ipDownload, $timeDownload, $time1Download, $time2Download, $webId, $serviceId) {
        if (!empty($time1Download) && !empty($time2Download)) {
            $this->db->where("time BETWEEN '$time1Download' AND '$time2Download'");
        }
        if (!empty($webId)) {
            $where['cid'] = $webId;
        }
        if (!empty($serviceId)) {
            $where['service_id'] = $serviceId;
        }
        if (!empty($ipDownload)) {
            $where['ip'] = $ipDownload;
        }
        if (!empty($timeDownload)) {
            $where['time'] = $timeDownload;
        }
        if (!empty($where)) {
            $this->db->where($where);
        }
        $re = $this->db
                ->get('log')
                ->result_array();
        return $re;
    }

    public function getMonth() {
        $year = date('Y');
        $day = "01";
        for ($i = 1, $j = 2; $i <= 12, $j <= 13; $i++, $j++) {
            if ($i < 10) {
                $month1 = $year . '-0' . $i . '-' . $day;
            } else {
                $month1 = $year . '-' . $i . '-' . $day;
            }
            if ($j < 10) {
                $month2 = $year . '-0' . $j . '-' . $day;
            } else {
                $month2 = $year . '-' . $j . '-' . $day;
            }
            $re['data'][] = $this->db->where("time BETWEEN '$month1' AND '$month2'")->get('log')->num_rows();
        }
        return $re;
    }

    public function getDay($month) {
        $year = date('Y');
        if ($month < 10) {
            $month = $year . '-0' . $month;
        } else {
            $month = $year . '-' . $month;
        }
        for ($i = 1, $j = 2; $i <= 31, $j <= 32; $i++, $j++) {
            if ($i < 10) {
                $day = $month . '-0' . $i;
            } else {
                $day = $month . '-' . $i;
            }
            if ($j < 10) {
                $day2 = $month . '-0' . $j;
            } else {
                $day2 = $month . '-' . $j;
            }
            $re['data'][] = $this->db->where("time BETWEEN '$day' AND '$day2'")->get('log')->num_rows();
        }
        return $re;
    }

    public function getHour($month, $day) {
        $year = date('Y');
        if ($month < 10) {
            $month = $year . '-0' . $month;
        } else {
            $month = $year . '-' . $month;
        }
        if ($day < 10) {
            $day = $month . '-0' . $day;
        } else {
            $day = $month . '-' . $day;
        }
        for ($i = 0, $j = 1; $i < 24, $j < 25; $i++, $j++) {
            if ($i < 10) {
                $hour = $day . ' 0' . $i . ':00:00';
            } else {
                $hour = $day . ' ' . $i . ':00:00';
            }
            if ($j < 10) {
                $hour2 = $day . ' 0' . $j . ':00:00';
            } else {
                $hour2 = $day . ' ' . $j . ':00:00';
            }
            $re['data'][] = $this->db->where("time BETWEEN '$hour' AND '$hour2'")->get('log')->num_rows();
        }
        return $re;
    }

    public function getIP() {
        $re = $this->db->select('ip')->where("time BETWEEN '2009-01-28 00:00:00' AND '2016-09-28 14:06:29'")->get('log')->result_array();
        foreach ($re as $key => $val) {
            $re[$key] = $val['ip'];
        }
        $re = array_count_values($re);

        foreach ($re as $ip => $val) {
            $formatRe['data'][] = array('name' => $ip, 'value' => $val);
            $json = json_decode(file_get_contents('http://api.map.baidu.com/location/ip?ak=DC424da237a7405fdcbc8770f91d655a&coor=bd09ll&ip=' . $ip), true);
            if (isset($json['content']['point'])) {
                $point = $json["content"]['point'];
                $formatRe['geo'][$ip] = array($point['x'], $point['y']);
            }
        }

        return $formatRe;
    }

    public function getBarChart() {
        $re = $this->db->select('ip')->where("time BETWEEN '2009-01-28 00:00:00' AND '2016-09-28 14:06:29'")->get('log')->result_array();
        foreach ($re as $key => $val) {
            $re[$key] = $val['ip'];
        }
        $re = array_count_values($re);

        foreach ($re as $ip => $val) {
//            $json = json_decode(file_get_contents('http://api.map.baidu.com/location/ip?ak=DC424da237a7405fdcbc8770f91d655a&coor=bd09ll&ip=' . $ip), true);
//            if (isset($json['content']['address'])) {
//                $value[$json['content']['address']] = @$value[$json['content']['address']] + $re[$ip];
//            }
            $this->load->library('Ip');
            $address = $this->ip->find($ip);
            if (isset($address[2]) && !empty($address[2])) {
                $value[$address[2]] = @$value[$address[2]] + $re[$ip];
            } else if (isset($address[1]) && !empty($address[1])) {
                $value[$address[1]] = @$value[$address[1]] + $re[$ip];
            }
        }

        foreach ($value as $address => $val) {
            $formatRe['data'][] = $val;
            $formatRe['category'][] = $address;
        }
        return $formatRe;
    }

}
